<?php

namespace App\Observers;

use App\Models\ProductPes;

class ProductPesObserver
{
    public function saved(ProductPes $productPes)
    {
        $this->balanceProductPes($productPes);
        $this->updateTotalStockAndMinExpirationDate($productPes);
    }

//    public function updated(ProductPes $productPes)
//    {
//        $this->updateTotalStockAndMinExpirationDate($productPes);
//    }

    public function deleted(ProductPes $productPes)
    {
        $this->balanceProductPes($productPes);
        $this->updateTotalStockAndMinExpirationDate($productPes);
    }

    protected function updateTotalStockAndMinExpirationDate($productPes)
    {
        $product = $productPes->product;
        $productPes = $product->productPesWithoutSoldOutChecked;
        $product->min_expiration_date = $productPes->where('stock', '<>', 0)->min('expiration_date');
        $total_stock = $productPes->sum('stock');
        $total_registered_stock = $productPes->sum('registered_stock');
        $product->total_stock = $total_stock;
        $product->total_registered_stock = $total_registered_stock;
        $product->warehouse_stock = $total_stock - $product->vending_machine_stock;
        $product->save();
    }

    protected function balanceProductPes($productPes)
    {
        $product = $productPes->product;
        $productPes = $product->productPesWithoutSoldOutChecked;
        $negativeProductPes = $productPes->where('stock', '<', 0)->first();
        if ($negativeProductPes) {
            $positiveProductPes = $productPes->where('stock', '>', 0)->first();
            $positiveProductPesCount = $productPes->where('stock', '>', 0)->count();
            if ($positiveProductPes) {
                if (abs($negativeProductPes->stock) <= $positiveProductPes->stock) {
//                    $positiveProductPes->stock += $negativeProductPes->stock;
//                    $negativeProductPes->stock = 0;
//                    $positiveProductPes->save();
//                    $negativeProductPes->save();
                    $positiveProductPes->decrement('stock', abs($negativeProductPes->stock));
                    $negativeProductPes->increment('stock', abs($negativeProductPes->stock));
                } elseif (abs($negativeProductPes->stock) > $positiveProductPes->stock && $positiveProductPesCount >= 1) {
                    do {
//                        $negativeProductPes->stock += $positiveProductPes->stock;
//                        $positiveProductPes->stock = 0;
//                        $positiveProductPes->save();
//                        $negativeProductPes->save();
                        $negativeProductPes->increment('stock', $positiveProductPes->stock);
                        $positiveProductPes->decrement('stock', $positiveProductPes->stock);
                        $negativeProductPes->refresh();
                        $positiveProductPes = $productPes->where('stock', '>', 0)->first();
                    } while ($positiveProductPes && abs($negativeProductPes->stock) > $positiveProductPes->stock);
                    if ($positiveProductPes) {
//                        $positiveProductPes->stock += $negativeProductPes->stock;
//                        $negativeProductPes->stock = 0;
//                        $negativeProductPes->save();
//                        $positiveProductPes->save();
                        $positiveProductPes->decrement('stock', abs($negativeProductPes->stock));
                        $negativeProductPes->increment('stock', abs($negativeProductPes->stock));
                    }
                }
            }
        }
    }
}
